package com.sh.data.engine.domain.shims.saphana.manager;

import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.sap.db.jdbc.HanaClob;
import com.sh.data.engine.domain.base.model.enums.Database;
import com.sh.data.engine.domain.shims.db.DbOptions;
import com.sh.data.engine.domain.shims.db.GenericDbManager;
import com.sh.data.engine.domain.shims.db.model.FieldInfoDomain;
import com.sh.data.engine.domain.shims.db.model.PreviewDataDomain;
import com.sh.data.engine.domain.shims.db.model.TableInfoDomain;
import com.sh.data.engine.domain.shims.db.model.TableSizeDomain;
import com.sh.data.engine.domain.shims.sql.select.MapRowMapper;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import java.sql.*;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author: mengzheng.mhc
 * @date: 2024/6/6 10:45
 */
public class SapManager extends GenericDbManager {

    public SapManager(DbOptions options) {
        super(options);
    }

    static {
        String driverClass = "com.sap.db.jdbc.Driver";
        try {
            Class.forName(driverClass);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException("Could not load db driver class: " + driverClass);
        }
    }

    @Override
    public String getQueryVersionSql() {
        return "select VERSION  from \"SYS\".\"M_DATABASE\"";
    }

    @Override
    public PreviewDataDomain previewData(String schema, String tableName) throws SQLException {
        return super.previewData(escapeTableName(schema) + "." + escapeTableName(tableName));
    }

    @Override
    public List<TableInfoDomain> getTableList(String dbName) throws SQLException {
        String sql =
            "SELECT schema_name ,table_name ,table_oid ,comments,'table' AS table_type FROM tables WHERE schema_name NOT IN ('SYS','SYS_DATABASES') AND schema_name NOT LIKE '_SYS%'";

        List<TableInfoDomain> list = new LinkedList<>();

        try (Connection conn = getConnection();
             PreparedStatement pstmt = createPreparedStatement(conn, sql);
             ResultSet resultSet = pstmt.executeQuery()) {
            while (resultSet != null && resultSet.next()) {
                TableInfoDomain tableInfo = getTableDomain(dbName, resultSet);
                list.add(tableInfo);
            }
        }
        return list;
    }

    @Override
    public List<TableInfoDomain> getTableList(String dbName, String schema) throws SQLException {
        String sql =
            "SELECT schema_name ,table_name ,table_oid ,comments, 'table' AS table_type FROM tables WHERE schema_name = ?";

        List<TableInfoDomain> list = new LinkedList<>();

        try (Connection conn = getConnection();
             PreparedStatement pstmt = createPreparedStatement(conn, sql, schema);
             ResultSet resultSet = pstmt.executeQuery()) {
            while (resultSet != null && resultSet.next()) {
                TableInfoDomain tableInfo = getTableDomain(dbName, resultSet);
                list.add(tableInfo);
            }
        }
        return list;
    }

    private static TableInfoDomain getTableDomain(String dbName, ResultSet rs) throws SQLException {
        ResultSetMetaData meta = rs.getMetaData();
        Set<String> set = new HashSet<>();
        for (int index = 1; index <= meta.getColumnCount(); index++) {
            set.add(meta.getColumnLabel(index));
        }
        TableInfoDomain entity = new TableInfoDomain();
        entity.setDbName(dbName);

        if (set.contains("SCHEMA_NAME")) {
            entity.setSchema(rs.getString("schema_name"));
        }

        if (set.contains("TABLE_NAME")) {
            String tblName = rs.getString("TABLE_NAME");
            entity.setTableName(tblName);
        }
        if (set.contains("TABLE_OID")) {
            entity.setTblId(rs.getLong("TABLE_OID"));
        }

        if (set.contains("TABLE_TYPE")) {
            String tableType = rs.getString("TABLE_TYPE");
            TableInfoDomain.TblType tblType = null;
            switch (tableType) {
                case "table":
                    tblType = TableInfoDomain.TblType.MANAGED_TABLE;
                    break;
                case "view":
                    tblType = TableInfoDomain.TblType.VIEW;
                    break;
            }
            entity.setTblType(tblType);
        }

        // 视图的create_time和update_time都是空的
//    if (set.contains("CREATE_TIME")) {
//      Date updateTime = rs.getDate("CREATE_TIME");
//      if (null != updateTime) {
//        entity.setLastDdlTime(updateTime.getTime());
//      }
//    }

        if (set.contains("COMMENTS")) {
            final String tableComment = rs.getString("COMMENTS");
            entity.setTableComment(tableComment);
        }

        return entity;
    }

    private static FieldInfoDomain getFieldInfo(ResultSet rs) throws SQLException {
        ResultSetMetaData meta = rs.getMetaData();
        Set<String> set = new HashSet<>();
        FieldInfoDomain entity = new FieldInfoDomain();
        for (int index = 1; index <= meta.getColumnCount(); index++) {
            set.add(meta.getColumnLabel(index));
        }
        if (set.contains("COLUMN_NAME")) {
            entity.setFieldName(rs.getString("COLUMN_NAME"));
        }
        if (set.contains("DATA_TYPE_ID")) {
            entity.setDataType(rs.getInt("DATA_TYPE_ID"));
        }
        if (set.contains("DATA_TYPE_NAME")) {
            entity.setFieldType(rs.getString("DATA_TYPE_NAME"));
        }
        if (set.contains("LENGTH")) {
            entity.setColumnSize(rs.getInt("LENGTH"));
        }
        if (set.contains("COMMENTS")) {
            String comments = rs.getString("COMMENTS");
            entity.setComment(comments);
        }
        if (set.contains("IS_NULLABLE")) {
            entity.setNullable(rs.getBoolean("IS_NULLABLE"));
        }
        return entity;
    }

    private HashSet<String> getSapPrimaryKeys(String tableName, String schemaName)
        throws SQLException {
        if (StringUtils.isBlank(schemaName)) {
            return getSapPrimaryKeys(tableName);
        }
        HashSet<String> rs = Sets.newHashSet();
        String sql =
            "SELECT C.COLUMN_NAME FROM sys.\"CONSTRAINTS\" c WHERE c.IS_PRIMARY_KEY ='TRUE' and c.TABLE_NAME = ? and c.SCHEMA_NAME = ?";
        try (Connection conn = getConnection();
             PreparedStatement pstmt = createPreparedStatement(conn, sql, tableName, schemaName);
             ResultSet resultSet = pstmt.executeQuery()) {
            ResultSetMetaData meta = resultSet.getMetaData();
            Set<String> set = new HashSet<>();
            for (int index = 1; index <= meta.getColumnCount(); index++) {
                set.add(meta.getColumnLabel(index));
            }
            while (resultSet != null && resultSet.next()) {
                if (set.contains("COLUMN_NAME")) {
                    rs.add(resultSet.getString("COLUMN_NAME"));
                }
            }
        }
        return rs;
    }

    private HashSet<String> getSapPrimaryKeys(String tableName) throws SQLException {
        HashSet<String> rs = Sets.newHashSet();
        String sql =
            "SELECT C.COLUMN_NAME FROM sys.\"CONSTRAINTS\" c WHERE c.IS_PRIMARY_KEY ='TRUE' and c.TABLE_NAME = ?";
        try (Connection conn = getConnection();
             PreparedStatement pstmt = createPreparedStatement(conn, sql, tableName);
             ResultSet resultSet = pstmt.executeQuery()) {
            ResultSetMetaData meta = resultSet.getMetaData();
            Set<String> set = new HashSet<>();
            for (int index = 1; index <= meta.getColumnCount(); index++) {
                set.add(meta.getColumnLabel(index));
            }
            while (resultSet != null && resultSet.next()) {
                if (set.contains("COLUMN_NAME")) {
                    rs.add(resultSet.getString("COLUMN_NAME"));
                }
            }
        }
        return rs;
    }

    @Override
    public List<TableInfoDomain> getTableList(String dbName, List<String> tableNameLists)
        throws SQLException {
        if (CollectionUtils.isEmpty(tableNameLists)) {
            return Lists.newArrayList();
        }
        String sql =
            "SELECT schema_name ,table_name ,table_oid ,comments  ,'table' AS table_type FROM tables WHERE table_name in ($table)";
        List<TableInfoDomain> list = new LinkedList<>();

        tableNameLists =
            tableNameLists.stream().map(item -> "'" + item + "'").collect(Collectors.toList());

        String tmpSql = sql.replace("$table", String.join(",", tableNameLists));
        try (Connection conn = getConnection();
             PreparedStatement pstmt = conn.prepareStatement(tmpSql);
             ResultSet resultSet = pstmt.executeQuery()) {
            while (resultSet != null && resultSet.next()) {
                TableInfoDomain tableInfo = getTableDomain(null, resultSet);
                list.add(tableInfo);
            }
        }
        return list;
    }

    @Override
    public List<TableInfoDomain> getViewList(String dbName, List<String> viewNameLists)
        throws SQLException {
        if (CollectionUtils.isEmpty(viewNameLists)) {
            return Lists.newArrayList();
        }
        String sql =
            "SELECT schema_name ,view_name AS table_name ,view_oid AS table_oid,comments  ,'view' AS table_type FROM views WHERE view_name in ($view)";
        List<TableInfoDomain> list = new LinkedList<>();
        viewNameLists =
            viewNameLists.stream().map(item -> "'" + item + "'").collect(Collectors.toList());
        String tmpSql = sql.replace("$view", String.join(",", viewNameLists));
        try (Connection conn = getConnection();
             PreparedStatement pstmt = conn.prepareStatement(tmpSql);
             ResultSet resultSet = pstmt.executeQuery()) {
            while (resultSet != null && resultSet.next()) {
                TableInfoDomain tableInfo = getTableDomain(null, resultSet);
                list.add(tableInfo);
            }
        }

        return list;
    }

    @Override
    public List<TableInfoDomain> getViewList(String dbName) throws SQLException {
        String sql =
            "SELECT schema_name ,view_name AS table_name ,view_oid AS table_oid,comments ,create_time ,'view' AS table_type FROM views WHERE schema_name NOT IN ('SYS','SYS_DATABASES') AND schema_name NOT LIKE '_SYS%'";
        List<TableInfoDomain> list = new LinkedList<>();

        try (Connection conn = getConnection();
             PreparedStatement pstmt = createPreparedStatement(conn, sql);
             ResultSet resultSet = pstmt.executeQuery()) {
            while (resultSet != null && resultSet.next()) {
                TableInfoDomain tableInfo = getTableDomain(dbName, resultSet);
                list.add(tableInfo);
            }
        }
        return list;
    }

    @Override
    public List<TableInfoDomain> getViewList(String dbName, String schema) throws SQLException {
        String sql =
            "SELECT schema_name ,view_name AS table_name ,view_oid AS table_oid,comments , 'view' AS table_type FROM views WHERE schema_name = ?";
        List<TableInfoDomain> list = new LinkedList<>();

        try (Connection conn = getConnection();
             PreparedStatement pstmt = createPreparedStatement(conn, sql, schema);
             ResultSet resultSet = pstmt.executeQuery()) {
            while (resultSet != null && resultSet.next()) {
                TableInfoDomain tableInfo = getTableDomain(dbName, resultSet);
                list.add(tableInfo);
            }
        }
        return list;
    }

    @Override
    public List<TableInfoDomain> getTableAndViewList(String dbName) throws SQLException {
        String sql =
            "SELECT schema_name ,table_name ,table_oid ,comments, 'table' AS table_type FROM tables WHERE schema_name NOT IN ('SYS','SYS_DATABASES') AND schema_name NOT LIKE '_SYS%' "
                + "UNION all "
                + "SELECT schema_name ,view_name AS table_name ,view_oid AS table_oid,comments  ,'view' AS table_type FROM views WHERE schema_name NOT IN ('SYS','SYS_DATABASES') AND schema_name NOT LIKE '_SYS%'";
        List<TableInfoDomain> list = new LinkedList<>();

        try (Connection conn = getConnection();
             PreparedStatement pstmt = createPreparedStatement(conn, sql);
             ResultSet resultSet = pstmt.executeQuery()) {
            while (resultSet != null && resultSet.next()) {
                TableInfoDomain tableInfo = getTableDomain(dbName, resultSet);
                list.add(tableInfo);
            }
        }
        return list;
    }

    @Override
    public List<TableInfoDomain> getTableAndViewList(String dbName, String schema)
        throws SQLException {

        if (StringUtils.isBlank(schema)) {
            return getTableAndViewList(dbName);
        }
        String sql =
            "SELECT schema_name ,table_name ,table_oid ,comments, 'table' AS table_type FROM tables WHERE schema_name = ? "
                + "UNION all "
                + "SELECT schema_name ,view_name AS table_name ,view_oid AS table_oid,comments  ,'view' AS table_type FROM views WHERE schema_name = ?";
        List<TableInfoDomain> list = new LinkedList<>();

        try (Connection conn = getConnection();
             PreparedStatement pstmt = createPreparedStatement(conn, sql, schema, schema);
             ResultSet resultSet = pstmt.executeQuery()) {
            while (resultSet != null && resultSet.next()) {
                TableInfoDomain tableInfo = getTableDomain(dbName, resultSet);
                list.add(tableInfo);
            }
        }
        return list;
    }

    // 这个方法别再改了
    @Override
    public List<TableSizeDomain> getTableSizeList(String dbName, String schema) throws SQLException {
        String sql =
            "SELECT schema_name,table_name ,disk_size FROM PUBLIC.M_TABLE_PERSISTENCE_STATISTICS WHERE schema_name NOT IN ('SYS','SYS_DATABASES') AND schema_name NOT LIKE '_SYS%'";

        List<TableSizeDomain> list = new ArrayList<>();

        try (Connection connection = getConnection();
             PreparedStatement pstmt = connection.prepareStatement(sql);
             ResultSet rs = pstmt.executeQuery()) {

            while (rs.next()) {
                TableSizeDomain domain = new TableSizeDomain();
                final String schemaName = rs.getString("schema_name");
                final String tableName = rs.getString("table_name");
                final long size = rs.getLong("disk_size");
                domain.setTableSize(size);
                domain.setTableName(tableName);
                domain.setSchema(schemaName);
                list.add(domain);
            }
            return list;
        }
    }

    @Override
    public TableInfoDomain getTableInfoByTableName(String dbName, String tableName)
        throws SQLException {
        if (StringUtils.isNotBlank(dbOptions.getSchema())) {
            return getTableInfoByTableName(dbName, dbOptions.getSchema(), tableName);
        }

        String sql =
            "SELECT schema_name ,table_name ,table_oid ,comments,'table' AS table_type FROM tables WHERE "
                + "table_name = ? "
                + "union SELECT schema_name ,view_name ,view_oid ,comments,'view' AS table_type FROM views WHERE view_name = ?";

        List<TableInfoDomain> list = new LinkedList<>();
        try (Connection conn = getConnection();
             PreparedStatement pstmt = createPreparedStatement(conn, sql, tableName, tableName);
             ResultSet resultSet = pstmt.executeQuery()) {
            while (resultSet != null && resultSet.next()) {
                TableInfoDomain tableInfo = getTableDomain(dbName, resultSet);
                list.add(tableInfo);
            }
        }
        return list.isEmpty() ? null : list.get(0);
    }

    @Override
    public TableInfoDomain getTableInfoByTableName(String dbName, String schema, String tableName)
        throws SQLException {
        String sql =
            "SELECT schema_name ,table_name ,table_oid ,comments, 'table' AS table_type FROM tables WHERE schema_name = ? "
                + "and table_name = ? "
                + "union SELECT schema_name ,view_name ,view_oid ,comments,'view' AS table_type FROM views WHERE schema_name = ? and view_name = ?";

        List<TableInfoDomain> list = new LinkedList<>();
        try (Connection conn = getConnection();
             PreparedStatement pstmt =
                 createPreparedStatement(conn, sql, schema, tableName, schema, tableName);
             ResultSet resultSet = pstmt.executeQuery()) {
            while (resultSet != null && resultSet.next()) {
                TableInfoDomain tableInfo = getTableDomain(dbName, resultSet);
                list.add(tableInfo);
            }
        }
        return list.isEmpty() ? null : list.get(0);
    }

    @Override
    public List<FieldInfoDomain> getFieldList(String dbName, String tableName) throws SQLException {
        if (StringUtils.isNotBlank(dbOptions.getSchema())) {
            return getFieldList(dbName, dbOptions.getSchema(), tableName);
        }

        String sql =
            "SELECT SCHEMA_NAME,TABLE_NAME,COLUMN_NAME,DATA_TYPE_ID,DATA_TYPE_NAME,LENGTH,COMMENTS,IS_NULLABLE,INDEX_TYPE,POSITION FROM SYS.TABLE_COLUMNS WHERE TABLE_NAME = ? "
                + "UNION "
                + "SELECT SCHEMA_NAME,VIEW_NAME ,COLUMN_NAME,DATA_TYPE_ID,DATA_TYPE_NAME,LENGTH,COMMENTS,IS_NULLABLE,INDEX_TYPE,POSITION FROM SYS.VIEW_COLUMNS WHERE VIEW_NAME = ? "
                + "ORDER BY POSITION";
        ArrayList<FieldInfoDomain> fieldInfos = Lists.newArrayList();
        try (Connection conn = getConnection();
             PreparedStatement pstmt = createPreparedStatement(conn, sql, tableName, tableName);
             ResultSet resultSet = pstmt.executeQuery()) {
            while (resultSet != null && resultSet.next()) {
                FieldInfoDomain fieldInfo = getFieldInfo(resultSet);
                fieldInfos.add(fieldInfo);
            }
        }
        return fieldInfos;
    }

    @Override
    public List<FieldInfoDomain> getFieldList(String dbName, String schema, String tableName)
        throws SQLException {
        String sql =
            "SELECT SCHEMA_NAME,TABLE_NAME,COLUMN_NAME,DATA_TYPE_ID,DATA_TYPE_NAME,LENGTH,COMMENTS,IS_NULLABLE,INDEX_TYPE,POSITION FROM SYS.TABLE_COLUMNS WHERE SCHEMA_NAME = ? AND TABLE_NAME = ? "
                + "UNION "
                + "SELECT SCHEMA_NAME,VIEW_NAME ,COLUMN_NAME,DATA_TYPE_ID,DATA_TYPE_NAME,LENGTH,COMMENTS,IS_NULLABLE,INDEX_TYPE,POSITION FROM SYS.VIEW_COLUMNS WHERE SCHEMA_NAME = ? AND VIEW_NAME = ? "
                + "ORDER BY POSITION";
        ArrayList<FieldInfoDomain> fieldInfos = Lists.newArrayList();
        try (Connection conn = getConnection();
             PreparedStatement pstmt =
                 createPreparedStatement(conn, sql, schema, tableName, schema, tableName);
             ResultSet resultSet = pstmt.executeQuery()) {
            while (resultSet != null && resultSet.next()) {
                FieldInfoDomain fieldInfo = getFieldInfo(resultSet);
                fieldInfos.add(fieldInfo);
            }
        }

        HashSet<String> sapPrimaryKeys = getSapPrimaryKeys(tableName, schema);
        fieldInfos.stream()
            .forEach(
                e -> {
                    if (sapPrimaryKeys.contains(e.getFieldName())) {
                        e.setPk(true);
                    }
                });
        return fieldInfos;
    }

    @Override
    public boolean alterFieldDesc(
        String dbName, String tableName, String fieldName, String fieldDes, boolean isPartition)
        throws SQLException {
        throw new SQLException("暂未实现");
    }

    @Override
    public String getQuerySchemaListSql() {
        String sql =
            "SELECT SCHEMA_NAME FROM schemas WHERE instr(SCHEMA_NAME,'_SYS')=0 AND SCHEMA_NAME!='SYS'";
        return sql;
    }

    @Override
    public void testConnection() throws SQLException {
        Integer timeout = dbOptions.getLoginTimeout();
        if (timeout == null) {
            timeout = 10;
        }
        int loginTimeout = DriverManager.getLoginTimeout();
        // 再设置成默认值，以防对其他地方有影响
        DriverManager.setLoginTimeout(timeout);
        try (Connection conn = getConnection()) {
        } catch (SQLException e) {
            throw new SQLException(toReadableMsg(e), e);
        } finally {
            DriverManager.setLoginTimeout(loginTimeout);
        }
    }

    public Boolean checkSchema(String schema) throws SQLException {
        List<String> schemaList = getSchemaList();
        return schemaList.contains(schema);
    }

    public static void main(String[] args) throws Exception {
        DbOptions dbOptions = new DbOptions();
        dbOptions.setUrl("jdbc:sap://10.88.36.79:39041");
        dbOptions.setUsername("SYSTEM");
        dbOptions.setPassword("HXEHana1");
        dbOptions.setSchema("TEST");
        dbOptions.setDatabase(Database.SAP);
        SapManager sapManager = new SapManager(dbOptions);

        final Connection connection = sapManager.getConnection();

        String sql =
            "SELECT t.SCHEMA_NAME ,t.VIEW_NAME ,t.CREATE_TIME ,t.DEFINITION  FROM sys.views t WHERE t.VIEW_NAME LIKE '%DIM%'";

        sql = "SELECT * FROM \"TEST\".\"ods_tmp_68ahy3\"";

        final PreparedStatement preparedStatement = connection.prepareStatement(sql);

        final ResultSet resultSet = preparedStatement.executeQuery();

        List<Map<String, Object>> result = new LinkedList<>();
        MapRowMapper rowMapper = new MapRowMapper();
        int rowNum = 0;
        while (resultSet.next()) {

            Map<String, Object> convert = rowMapper.convert(resultSet, rowNum++);
            Set<String> keySet = convert.keySet();
            for (String key : keySet) {
                //
                Object value = convert.get(key);
                if (value instanceof HanaClob) {
//          HanaClob hanaClob = (HanaClob) value;
//          final Reader reader = hanaClob.getCharacterStream();
//
//          BufferedReader bufferedReader = new BufferedReader(reader);
//
//          StringBuffer sb = new StringBuffer();
//
//          String temp = null;
//
//          while ((temp = bufferedReader.readLine()) != null) {
//
//            sb.append(temp);
//          }
//
//          bufferedReader.close();
//
//          System.out.println(sb);
                } else {
                    System.out.println(value);
                }
            }
            System.out.println("=================================");
            result.add(convert);
        }

        // 要把hanaClob转化为String
        for (Map<String, Object> data : result) {
            Set<String> keySet = data.keySet();
            for (String key : keySet) {
                Object value = data.get(key);
                if (value instanceof HanaClob) {
                    HanaClob hanaClob = (HanaClob) value;
                    System.out.println(
                        "hanaClob.getSubString(1L, Math.toIntExact(hanaClob.length())) = "
                            + hanaClob.getSubString(1L, Math.toIntExact(hanaClob.length())));
//          final Reader reader = hanaClob.getCharacterStream();
//
//          BufferedReader bufferedReader = new BufferedReader(reader);
//
//          StringBuffer sb = new StringBuffer();
//
//          String temp = null;
//
//          while ((temp = bufferedReader.readLine()) != null) {
//
//            sb.append(temp);
//          }
//
//          bufferedReader.close();

                    //System.out.println(sb);
                } else {
                    System.out.println(value);
                }
            }
        }

//    while (resultSet.next()) {
//      final Object object = resultSet.getObject(1);
//      HanaClob hanaClob = (HanaClob) object;
//      final Reader reader = hanaClob.getCharacterStream();
//
//      BufferedReader bufferedReader = new BufferedReader(reader);
//
//      StringBuffer sb = new StringBuffer();
//
//      String temp = null;
//
//      while ((temp = bufferedReader.readLine()) != null) {
//
//        sb.append(temp);
//      }
//
//      bufferedReader.close();
//
//      System.out.println(sb);
//    }

        //    List<TableInfoDomain> tableAndViewList = sapManager.getTableAndViewList("");
        //    System.out.println(tableAndViewList);
        //    System.out.println(DSType.Sap.name());

        //    String table = "schema.table.table2";
        //    String[] split = table.split("\\.", 2);
        //    System.out.println(split[0] + "   " + split[1]);
        //    long start = System.currentTimeMillis();
        //    List<TableInfoDomain> tableAndViewList = sapManager.getTableAndViewList("");
        //    long end = System.currentTimeMillis();
        //    System.out.println((end-start)/1000+"s");
        //    System.out.println(tableAndViewList.size());
        //    DDLBuilder ddlBuilder = sapManager.getDDLBuilder();
        //    CreateTableParam param = new CreateTableParam();
        //    param.setSchema("SYSTEM");
        //    CreateTableParam.FieldInfo fieldInfo = new CreateTableParam.FieldInfo();
        //    fieldInfo.setFieldName("ID");
        //    fieldInfo.setFieldType("INTEGER");
        //    param.setFieldInfoList(Lists.newArrayList(fieldInfo));
        //    int k=0;
        //    while (k<120000){
        //      param.setTableName(IdUtil.fastSimpleUUID());
        //      DDL ddl = ddlBuilder.create(param);
        //      ddl.execute();
        //      ddlBuilder.reset();
        //      k++;
        //      System.out.println(k);
        //    }

    }

    private Map<String, Object> convert(ResultSet resultSet, int rowNum) throws SQLException {
        ResultSetMetaData metaData = resultSet.getMetaData();

        Map<String, Object> rowMap = new LinkedHashMap<>();

        for (int i = 1; i <= metaData.getColumnCount(); i++) {
            rowMap.put(metaData.getColumnName(i), resultSet.getObject(i));
        }

        return rowMap;
    }
}
